package HttpClientUtil;

import com.alibaba.fastjson.JSONObject;

import Crawler_data.Crawler_data;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.proxy.Proxy;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.SocketAddress;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

@Component

public class IPJob {

	public static ArrayList<Proxy> proxies = new ArrayList<Proxy>();

	@Scheduled(cron="0/5 * *  * * ? ")  
	public void ipManage() {
		if (proxies.size() < 3) {
			// 初始化三个代理ip
			Proxy proxy = getProxy();
			try {
				if (checkIp(proxy)) {
					proxies.add(proxy);
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		} else {
			// 遍历集合 检查代理是否可用
			for (int i = 0; i < proxies.size(); i++) {
				Proxy proxy = proxies.get(i);
				try {
					if (!checkIp(proxy)) {
						proxies.remove(i);
						Proxy p = getProxy();
						if (p != null) {
							proxies.add(i, p);
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
					proxies.remove(i);
					Proxy p = getProxy();
					if (p != null) {
						proxies.add(i, p);
					}
				}

			}

		}
	}

	private Proxy getProxy() {
		Proxy proxy = null;
		try {
			for (int i = 0; !checkIp(proxy); i++) {
				String json = HttpClientUtil.doGet(
						"http://http.tiqu.alibabaapi.com/getip?num=1&type=2&pack=70177&port=1&lb=1&pb=4&regions=");
				//System.out.println(json);
				Map<String, Object> jsonMap = (Map<String, Object>) JSONObject.parse(json);
				List<Map<String, Object>> data = (List<Map<String, Object>>) jsonMap.get("data");
				Map<String, Object> proxyMap = data.get(0);
				String ip = proxyMap.get("ip").toString();
				String port = proxyMap.get("port").toString();
				proxy = new Proxy(ip, Integer.parseInt(port));

				// System.out.println(proxy);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

		return proxy;
	}

	/**
	 * 检查代理是否可用
	 * 
	 * @param proxy
	 * @return
	 */
	private boolean checkIp(Proxy proxy) throws Exception {
		boolean isValid = false;

		if (proxy == null) {
			return isValid;
		}

		String gmHtml = HttpClientUtil
				.doGetProxy("https://music.163.com/api/song/detail/?id=1846419185&ids=%5B1846419185%5D", proxy);

		System.out.println(gmHtml + "hhh");
//
//		Properties prop = System.getProperties();
//		// 设置HTTP访问要使用的代理服务器的地址
//		prop.setProperty("http.proxyHost", proxy.getHost());
//		// 设置HTTP访问要使用的代理服务器的端口
//		prop.setProperty("http.proxyPort", "" + proxy.getPort());
//		// java.net.Proxy proxy2 = new java.net.Proxy(proxy.getHost(), null);
//		URL music = new URL("http://music.163.com/song/media/outer/url?id=1846419185");
//
//		URLConnection connection = music.openConnection();
//		InputStream in = connection.getInputStream();
//		System.out.println(gmHtml.length() + "            "+in.available());
		if (gmHtml.length() > 100) {
			System.out.println("----------------------------------------");
			System.out.println(proxy);
		//	System.out.println(proxies);
			// && in != null && in.available() > 100 System.out.println(in.available());
			isValid = true;
		}
		return isValid;
	}

//	public static void main(String[] args) {
//		IPJob ipJob = new IPJob();
//		
////		while(true) {
////				ipJob.ipManage();	
////		}
//
//		
//	
//		Proxy proxy2 = new Proxy("125.111.146.84", 4345);
////		Proxy proxy3 = new Proxy("42.85.232.99", 4356);
////		Proxy proxy4 = new Proxy("122.195.216.20", 4307);
////		Proxy proxy5 = new Proxy("175.155.51.137", 4358);
////		Proxy proxy6 = new Proxy("117.63.131.188", 4376);
////		proxies.add(proxy);
////		proxies.add(proxy1);
//		proxies.add(proxy2);
////		proxies.add(proxy3);
////		proxies.add(proxy4);
////		proxies.add(proxy5);
////		proxies.add(proxy6);
//		//System.out.println(proxies);
//
//		for(Proxy proxy7 : proxies) {
//			try {
//				System.out.println(proxy7);
//				System.out.println(ipJob.checkIp(proxy7));;
//			} catch (Exception e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//
//		}
//			
//		
//	}

}
